package yt.kpool.activity.personal;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.activity.setting.ChangeActivity;
import yt.kpool.bean.BaseBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TimeCountUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * 忘记密码
 */
public class ForgetPwdActivity extends AppCompatActivity {

    @BindView(R.id.edit_phone)
    EditText edit_phone;
    @BindView(R.id.tv_vercode)
    EditText tv_vercode;
    @BindView(R.id.tv_verificationcode)
    TextView tv_verificationcode;

    private String tel="";
    private TimeCountUtil timeCountUtil;//时间计时

    private LoadingDialog loadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        initTitle();
    }

    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("找回密码");
        timeCountUtil = new TimeCountUtil(ForgetPwdActivity.this, 60000, 1000, tv_verificationcode, getResources().getColor(R.color.system_color));
        loadingDialog=new LoadingDialog(this,0);
    }

    @OnClick({R.id.btn_next, R.id.tv_verificationcode})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_next://下一步
                if (check()) {
                    validSmsCode();

                }

                break;
            case R.id.tv_verificationcode://发送验证码
                if (getPhone().length() != 11) {
                    ToastUtil.showToast(getApplicationContext(), getString(R.string.error_tel));

                } else {
                    tel=getPhone();
                    sendSMS();
                }
                break;

        }
    }

    /**
     * 发送验证码
     */
    private void sendSMS() {
        if (NetWorkUtil.check(this)) {

            //倒计时
            timeCountUtil.start();


            NetHelperNew.RetrievePassWordSendSmsCode(tel, new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {


                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                    Log.i("-->", "忘记密码发送验证码:" + baseBean.getContent());
                    if (baseBean.getType() == 1) {
                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());

                    } else {
                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }

    /**
     * 验证忘记密码验证码
     */
    private void validSmsCode() {
        if (NetWorkUtil.check(this)) {

            loadingDialog.show();
            NetHelperNew.RetrievePassWordValidSmsCode(tel,getvercode(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                    Log.i("-->", "验证忘记密码验证码:" + baseBean.getContent());
                    if (baseBean.getType() == 1) {


                        ForgetPwd2Activity.start(ForgetPwdActivity.this,tel,getvercode());
                        finish();
                    } else {
                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }

    private String getPhone() {
        return edit_phone.getText().toString().trim();
    }

    /**
     * 获取验证码
     *
     * @return
     */
    private String getvercode() {
        return tv_vercode.getText().toString().trim();
    }

    private boolean check() {
        if (getPhone().length() != 11) {
            ToastUtil.showToast(getApplicationContext(), getString(R.string.error_tel));
            return false;
        }
        if (getvercode().length() == 0) {
            ToastUtil.showToast(getApplicationContext(), getString(R.string.error_vercode));
            return false;
        }
        return true;
    }
}
